import axios from 'axios'
import { getToken, setToken, removeToken } from '@/utils/auth'


const user = {
    
    state: {
        token: getToken(),
        name: '',
        school: '',
        roles: [],
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_SCHOOL: (state, school) => {
            state.school = school
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
    },
    actions: {
        //获取用户信息
        GetInfo({ commit, state }) {
            // return axios.get('/user').then(response => {
            //     let res = response.data
            //     if (res.code == 200) {
            //         let roles = []
            //         roles.push(res.data.roleCode)
            //         commit('SET_ROLES', roles) 
            //         commit('SET_NAME', res.data.name)
            //         if(res.data.schoolName){
            //             commit('SET_SCHOOL', res.data.schoolName) 
            //         }
            //     } else if (res.code == 401 || res.code == 404) {
            //         commit('SET_TOKEN', '')
            //         removeToken()
            //     }
            // }).catch(error => {
            //     console.log(error)
            // })
            
        },

     // 登出
        LogOut({ commit, state }) {
            // return axios.get('/certificate/logout').then(response => {
            //     let res = response.data
            //     if (res.code == 200) {
            //         commit('SET_TOKEN', '')
            //         removeToken()
            //         window.location.reload()
            //     } else if (res.code == 401) {
            //         commit('SET_TOKEN', '')
            //         removeToken()
            //     }
            // }).catch(error => {
            //     console.log(error)
            // })   
        }
    },
    
   
}

export default user

